package com.exmape.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;
import java.util.Set;

/**
 * @author dzii
 */
public class MybatisGenerator {

    /**
     * 模块名称
     */
    static final String MODEL_NAME = "message";
    /**
     * 需要生成的表
     */
    static final Set<String> GENERATOR_TABLE_NAMES = Set.of(
            "t_message"
    );

    static final String USER_DIR = System.getProperty("user.dir");

    /**
     * 不常变动常量
     */
    static final String AUTHOR = "dzii";
    /**
     * .java 文件输出路径
     */
    static final String JAVA_DIR = USER_DIR + "\\generator\\java";
    /**
     * mapper.xml 文件输出路径
     */
    static final String MAPPER_DIR = USER_DIR + "\\generator\\resources\\mapper\\" + MODEL_NAME;
    /**
     * 数据库连接数据
     */
    static final String DATASOURCE_URL = "jdbc:mysql://ubuntu:3306/dubbo_demo?serverTimezone=GMT%2B8";
    /**
     * 数据连接名&密码
     */
    static final String DATASOURCE_USERNAME = "dubbo_demo";
    static final String DATASOURCE_PASSWORD = "dubbo_demo";
    /**
     * 包名
     */
    static final String PACKAGE_NAME = "com.example.dubbo.demo.server.modules";


    public static void main(String[] args) {
        generator();
        System.out.printf("文件已成功生成在【%s\\generator\\】内！", USER_DIR);
    }

    private static void generator() {
        FastAutoGenerator.create(DATASOURCE_URL, DATASOURCE_USERNAME, DATASOURCE_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
                            .dateType(DateType.TIME_PACK)
                            .commentDate("yyyy-MM-dd")
                            .disableOpenDir()
                            .outputDir(JAVA_DIR); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(PACKAGE_NAME) // 设置父包名
                            .moduleName(MODEL_NAME)
                            .pathInfo(Collections.singletonMap(OutputFile.xml, MAPPER_DIR)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    // 设置需要生成的表名
                    GENERATOR_TABLE_NAMES.forEach(builder::addInclude);
                    builder.enableSkipView()
                            .addTablePrefix("t_")
                            .entityBuilder()
                            .enableLombok()
                            .enableChainModel()
                            .enableTableFieldAnnotation()
                            .enableRemoveIsPrefix()
                            .superClass(BaseEntity.class)
                            .addSuperEntityColumns("id", "create_by", "create_time", "update_time")
                            .addTableFills(
                                    new Column("create_by", FieldFill.INSERT),
                                    new Column("create_time", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.INSERT_UPDATE)
                            )
                            .idType(IdType.ASSIGN_ID)
                            .naming(NamingStrategy.underline_to_camel)
                            .formatFileName("%sEntity")
                            .controllerBuilder()
                            .enableRestStyle()
                            .serviceBuilder()
                            .convertServiceFileName(entityName -> entityName + "Service")
                            .mapperBuilder()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .enableMapperAnnotation();
                })
                .templateConfig(
                        builder -> builder.entity("templates/entity.java")
                                .xml("templates/mapper.xml")
                                .mapper("templates/mapper.java")
                                .service("templates/service.java")
                                .serviceImpl("templates/serviceImpl.java")
                                .controller("templates/controller.java")
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
